/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.account.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.givebalance.entity.GiveBalance;
import com.jeeplus.modules.givebalance.service.GiveBalanceService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;

/**
 * 用户钱包Service
 * @author freedom
 * @version 2020-09-26
 */
@Service
@Transactional(readOnly = true)
public class AccountService extends CrudService<AccountMapper, Account> {
	@Autowired
	private GiveBalanceService giveBalanceService;
	@Autowired
	private MemberService memberService;

	public Account get(String id) {
		return super.get(id);
	}
	
	public List<Account> findList(Account account) {
		return super.findList(account);
	}
	
	public Page<Account> findPage(Page<Account> page, Account account) {
		String managerId = "";
		List<Account> accountList = new ArrayList<>();
		dataRuleFilter(account);
		account.setPage(page);
		if (account.getManager() != null){
			managerId = account.getManager().getId();
		}
		if (StringUtils.isNotBlank(managerId)){

			Member member1 = new Member();
			member1.setP(account.getManager());
//			member.setP(member.getManager());
			List<Member> memberList = memberService.findList(member1);
			//查询团队下级人员
			List<Member> queryList = new ArrayList<>();
			//最终集合
			List<Member> list = new ArrayList<>();
			queryList.addAll(memberList);
			list.addAll(memberList);
			while (list.size() > 0) {
				list.clear();
				List<Member> list1 = new ArrayList<>();
				for (Member member2 : queryList) {
					Member entity = new Member();
					entity.setP(member2);
//					member.setP(member2);
					List<Member> memberList1 = memberService.findList(entity);
					memberList.addAll(memberList1);
					list1.addAll(memberList1);
				}
				list.addAll(list1);
				queryList.clear();
				queryList.addAll(list1);
			}
			if (memberList.size() >0){
				for (Member member : memberList) {
					Account account1 = new Account();
					account1.setMember(member);
					List<Account> list1 = mapper.findAccountList(account1);
					accountList.addAll(list1);
				}
			}
			page.setCount(accountList.size());
			page.setList(accountList);
		}else {
			List<Account> list = mapper.findAccountList(account);
			page.setList(list);
		}

		return page;
//		return super.findPage(page, account);
	}
	
	@Transactional(readOnly = false)
	public void save(Account account) {
		super.save(account);
	}
	
	@Transactional(readOnly = false)
	public void delete(Account account) {
		super.delete(account);
	}
	public Double getTotalBuy() {
		Double money = mapper.getTotalBuy();
		return money;
	}

	/**
	 * 余额互转
	 * @param member
	 * @param tomem
	 * @param m
	 */
	@Transactional(readOnly = false)
	public synchronized void giveBalance(Member member, Member tomem, BigDecimal m) {
		//操作用户
		Account account = new Account();
		account.setMember(member);
		List<Account> accountList = super.findList(account);
		if (accountList.size()>0){
			BigDecimal balance = BigDecimal.valueOf(accountList.get(0).getYue());
			//手续费百分之3   12-12取消手续费
			accountList.get(0).setYue(balance.subtract(m).doubleValue());
			accountList.get(0).preUpdate();
			super.save(accountList.get(0));
		}
		//转交用户
		Account account1 = new Account();
		account1.setMember(tomem);
		List<Account> accountList1 = super.findList(account1);
		if (accountList1.size()>0){
			BigDecimal balance = BigDecimal.valueOf(accountList1.get(0).getYue());
			accountList1.get(0).setYue(balance.add(m).doubleValue());
			accountList1.get(0).preUpdate();
			super.save(accountList1.get(0));
		}
		//记录
		GiveBalance giveBalance = new GiveBalance();
		giveBalance.setMember(member);
		giveBalance.setTomember(tomem.getLoginName());
		giveBalance.setMoney(m.doubleValue());
		giveBalanceService.save(giveBalance);
	}

	public Double getTodayDynamic() {
		return mapper.getTodayDynamic();
	}

}